package com.cf.data.serve.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cf.data.serve.dto.HkMjDto;
import com.cf.data.serve.dto.PersonDto;
import com.cf.data.serve.entity.MjMange;
import com.cf.data.serve.entity.SbSuperBrain;
import com.cf.data.serve.mq.DirectRabbitMqConfig;
import com.cf.data.serve.service.IMjMangeService;
import com.cf.data.serve.service.ISbSuperBrainService;
import com.cf.data.serve.service.IssuedService;
import com.cf.data.serve.utils.Result;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 下发任务
 * @Author: cuixi
 * @Date:   2024-08-01
 * @Version: V1.0
 */
@Service
public class IssuedServiceImpl implements IssuedService {

    @Autowired
    private ISbSuperBrainService sbSuperBrainService;
    @Autowired
    private IMjMangeService mjMangeService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public Result<?> addFacePerson(PersonDto personDto) {
        SbSuperBrain sbSuperBrain = sbSuperBrainService.getById(personDto.getNvrId());
        personDto.setIp(sbSuperBrain.getIp());
        personDto.setPort(sbSuperBrain.getPort());
        personDto.setUsername(sbSuperBrain.getUsername());
        personDto.setPassword(sbSuperBrain.getPassword());
        rabbitTemplate.convertAndSend(DirectRabbitMqConfig.DIRECT_EXCHANGE_NAME, DirectRabbitMqConfig.DIRECT_ROUTING_KEY, JSONObject.toJSONString(personDto));
        return Result.ok();
    }

    @Override
    public Result<?> addMjPerson(HkMjDto hkMjDto) {
        String devId = hkMjDto.getDevId();
        MjMange mange = mjMangeService.getById(devId);
        hkMjDto.setIp(mange.getIp());
        hkMjDto.setPort(mange.getPort());
        hkMjDto.setUsername(mange.getUsername());
        hkMjDto.setPassword(mange.getPassword());
        hkMjDto.setXmId(mange.getXmId());
        String json = JSONObject.toJSONString(hkMjDto);
        rabbitTemplate.convertAndSend(DirectRabbitMqConfig.DIRECT_EXCHANGE_NAME, DirectRabbitMqConfig.DIRECT_ROUTING_KEY2, JSONObject.toJSONString(json));
        return Result.ok();
    }

    @Override
    public Result<?> removePerson(String userId) {
        JSONObject jsonObject = new JSONObject();
        //可能使用该队列多次，定义方法编码
        jsonObject.put("commonMethodId", "1");
        jsonObject.put("userId", userId);
        rabbitTemplate.convertAndSend(DirectRabbitMqConfig.DIRECT_EXCHANGE_NAME, DirectRabbitMqConfig.DIRECT_ROUTING_KEY3, JSONObject.toJSONString(jsonObject));
        return null;
    }

}
